package yl.hs.bmipfull.service.finance.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yl.hs.bmipfull.daos.finance.IBrickShipSettleDetailDao;
import yl.hs.bmipfull.daos.finance.IBrickShipSettleMainDao;
import yl.hs.bmipfull.daos.laboratory.IProductInfoDao;
import yl.hs.bmipfull.daos.production.IBrickShipOrderDao;
import yl.hs.bmipfull.daos.salemanage.ISaleOrderPlaceDetailDao;
import yl.hs.bmipfull.daos.system.IAppSettingDao;
import yl.hs.bmipfull.pojo.finance.ShipSettleMainEntity;
import yl.hs.bmipfull.pojo.production.ProductShipOrderEntity;
import yl.hs.bmipfull.service.finance.*;
import yl.hs.bmipfull.service.production.IProductShipOrderService;
import yl.hs.bmipfull.service.salemanage.ISaleContractService;
import yl.hs.bmipfull.utils.CK;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;
import yl.hs.bmipfull.viewmodels.finance.ContractAccountVO;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 张凯
 * @apiNote 合同台帐-ServiceImpl
 * @since 2022/1/11 2:57 下午 创建
 */
@Service
public class ContractAccountServiceImpl extends FinanceService implements IContractAccountService {

    @Autowired
    ISaleContractService saleContractService;

    @Autowired
    IShipSettleService shipSettleService;

    @Autowired
    IShipInvoiceRecordService invoiceRecordService;

    @Autowired
    IReceivePaymentRecordService paymentRecordService;

    @Autowired
    IProductShipOrderService productShipOrderService;

    @Autowired
    IProductInfoDao productInfoDao;
    @Autowired
    IAppSettingDao appSettingDao;
    @Autowired
    IBrickShipSettleMainDao brickShipSettleMainDao;
    @Autowired
    IBrickShipSettleDetailDao brickShipSettleDetailDao;
    @Autowired
    IBrickShipOrderDao brickShipOrderDao;
    @Autowired
    ISaleOrderPlaceDetailDao detailDao;
    @Override
    public PageResult<ContractAccountVO> queryList(PageQuery query, Integer page, Integer rowNum) throws Exception {
        var betonSN = HsApp.getSystemSet(CK.BetonSysCode);
        var glpsSN = HsApp.getSystemSet(CK.AggregateTakeoutSN);
        if (query.getQuery().get("productCateSn") == null) {
            throw new Exception("搜索条件中缺少productCateSn");
        }
        var saleContractList = saleContractService.queryPageByProductCateSn(query, page, rowNum);
        if (saleContractList.getData() == null) {
            throw new Exception("未找到相关合同信息");
        }
        List<ContractAccountVO> contractAccountList = new ArrayList<>();
        saleContractList.getData().forEach(t -> {
            ContractAccountVO accountEntity = new ContractAccountVO();
            accountEntity.setContractSn(t.getSn());
            accountEntity.setCustomerName(t.getCustomerName());
            ShipSettleMainEntity shipSettleMain = shipSettleService.findLastByContractSn(t.getSn());
            if (shipSettleMain != null) {
                accountEntity.setReceivableAmount(shipSettleMain.getTotalAmount());
                accountEntity.setTotalQuantity(shipSettleMain.getTotalQuantity());
            } else {
                accountEntity.setReceivableAmount(BigDecimal.ZERO);
                accountEntity.setTotalQuantity(BigDecimal.ZERO);
            }
            accountEntity.setInvoiceAmount(invoiceRecordService.sumInvoiceAmountByContractSn(t.getSn()));
            accountEntity.setReceivedAmount(paymentRecordService.sumAmountByContractSn(t.getSn()));
            accountEntity.setUncollectedAmount(accountEntity.getReceivableAmount().subtract(accountEntity.getReceivedAmount()));
//            if(accountEntity.getUncollectedAmount().compareTo(BigDecimal.ZERO)<0){
//                accountEntity.setUncollectedAmount(BigDecimal.ZERO);
//            }
            var shipOrderList = productShipOrderService.queryByContractSn(t.getSn());
            BigDecimal unsettledQuantity = BigDecimal.ZERO;
            BigDecimal unsettledAmount = BigDecimal.ZERO;
            for (var shipOrder : shipOrderList) {
                BigDecimal quantity;
                var amounts = shipOrder.getPrice();
                if (shipOrder.getState() >= 10) {
                    if (betonSN != null && betonSN.equals(shipOrder.getProductCateSN())) {
                        quantity = shipOrder.getShipmentReviewQuantity();
                    } else {
                        quantity = shipOrder.getShipmentReviewWeight();
                    }
                    if (glpsSN != null && glpsSN.equals(shipOrder.getProductCateSN())) {
                        if (shipOrder.getShipmentReviewPrice() != null && shipOrder.getShipmentReviewPrice().doubleValue() > 0) {
                            amounts = shipOrder.getShipmentReviewPrice();
                        }
                    }
                } else if (shipOrder.getState() == 8) {
                    if (betonSN != null && betonSN.equals(shipOrder.getProductCateSN())) {
                        quantity = shipOrder.getSignedQuantity();
                    } else {
                        quantity = shipOrder.getSignedWeight();
                    }
                } else {
                    if (betonSN != null && betonSN.equals(shipOrder.getProductCateSN())) {
                        quantity = shipOrder.getQuantity();
                    } else {
                        quantity = shipOrder.getNetWeight();
                    }
                }
                if (quantity == null) {
                    quantity = BigDecimal.ZERO;
                    log.info(shipOrder.getSn() + "-" + shipOrder.getShipmentReviewFlag() + "-" + shipOrder.getSigner());
                }
                unsettledQuantity = unsettledQuantity.add(quantity);

                BigDecimal amount = amounts == null ? BigDecimal.valueOf(0) : amounts.multiply(quantity);
                unsettledAmount = unsettledAmount.add(amount != null ? amount : BigDecimal.ZERO);
            }
            accountEntity.setUnsettledQuantity(unsettledQuantity);
            accountEntity.setUnsettledAmount(unsettledAmount);
            accountEntity.setContractBalance(accountEntity.getReceivedAmount().subtract(accountEntity.getReceivableAmount()).subtract(accountEntity.getUnsettledAmount()));
            contractAccountList.add(accountEntity);
        });
        PageResult<ContractAccountVO> result = new PageResult(rowNum);
        result.setRecords(saleContractList.getRecords());
        result.setData(contractAccountList);
        result.setTotal(result.getTotal());
        result.setPage(page);
        return result;
    }
    @Override
    public PageResult<ContractAccountVO> queryHfList(PageQuery query, Integer page, Integer rowNum) throws Exception {
        var betonSN = HsApp.getSystemSet(CK.BetonSysCode);
        var glpsSN = HsApp.getSystemSet(CK.AggregateTakeoutSN);
        var saleContractList = saleContractService.queryPageByProductCateSn(query, page, rowNum);
        if (saleContractList.getData() == null) {
            throw new Exception("未找到相关合同信息");
        }
        List<ContractAccountVO> contractAccountList = new ArrayList<>();
        saleContractList.getData().forEach(t -> {
            ContractAccountVO accountEntity = new ContractAccountVO();
            accountEntity.setContractSn(t.getSn());
            accountEntity.setCustomerName(t.getCustomerName());
            var  shipSettleMain = brickShipSettleMainDao.LastBrickSettle(t.getSn());
            if (shipSettleMain != null) {
                accountEntity.setReceivableAmount(shipSettleMain.getTotalAmount());
                accountEntity.setTotalQuantity(shipSettleMain.getCurrentVolume());
            } else {
                accountEntity.setReceivableAmount(BigDecimal.ZERO);
                accountEntity.setTotalQuantity(BigDecimal.ZERO);
            }
            accountEntity.setInvoiceAmount(invoiceRecordService.sumInvoiceAmountByContractSn(t.getSn()));
            accountEntity.setReceivedAmount(paymentRecordService.sumAmountByContractSn(t.getSn()));
            accountEntity.setUncollectedAmount(accountEntity.getReceivableAmount().subtract(accountEntity.getReceivedAmount()));
            var shipOrderList = brickShipOrderDao.queryListByContractSN(t.getSn());
            BigDecimal unsettledQuantity = BigDecimal.ZERO;
            BigDecimal unsettledAmount = BigDecimal.ZERO;
            for (var shipOrder : shipOrderList) {
                BigDecimal quantity;
                var amounts = shipOrder.getPrice();
                if (shipOrder.getState() >= 10) {
                    if (betonSN != null && betonSN.equals(shipOrder.getProductCateSN())) {
                        quantity = shipOrder.getShipmentReviewQuantity();
                    } else {
                        quantity = shipOrder.getShipmentReviewQuantity();
                    }
                    if (glpsSN != null && glpsSN.equals(shipOrder.getProductCateSN())) {
                        if (shipOrder.getShipmentReviewPrice() != null && shipOrder.getShipmentReviewPrice().doubleValue() > 0) {
                            amounts = shipOrder.getShipmentReviewPrice();
                        }
                    }
                } else if (shipOrder.getState() == 8) {
                    if (betonSN != null && betonSN.equals(shipOrder.getProductCateSN())) {
                        quantity = shipOrder.getSignedQuantity();
                    } else {
                        quantity = shipOrder.getSignedQuantity();
                    }
                } else {
                    if (betonSN != null && betonSN.equals(shipOrder.getProductCateSN())) {
                        quantity = shipOrder.getQuantity();
                    } else {
                        quantity = shipOrder.getQuantity();
                    }
                }
                if (quantity == null) {
                    quantity = BigDecimal.ZERO;
                    log.info(shipOrder.getSn() + "-" + shipOrder.getShipmentReviewFlag() + "-" + shipOrder.getSigner());
                }
                unsettledQuantity = unsettledQuantity.add(quantity);

                BigDecimal amount = amounts == null ? BigDecimal.valueOf(0) : amounts.multiply(quantity);
                unsettledAmount = unsettledAmount.add(amount != null ? amount : BigDecimal.ZERO);
            }
            accountEntity.setUnsettledQuantity(unsettledQuantity);
            accountEntity.setUnsettledAmount(unsettledAmount);
            accountEntity.setContractBalance(accountEntity.getReceivedAmount().subtract(accountEntity.getReceivableAmount()).subtract(accountEntity.getUnsettledAmount()).subtract(detailDao.findByContractSnMoney(t.getSn())));
            contractAccountList.add(accountEntity);
        });
        PageResult<ContractAccountVO> result = new PageResult(rowNum);
        result.setRecords(saleContractList.getRecords());
        result.setData(contractAccountList);
        result.setTotal(result.getTotal());
        result.setObject(contractAccountList.stream().map(ContractAccountVO::getContractBalance).reduce(BigDecimal::add));
        result.setPage(page);
        return result;
    }

    @Override
    public PageResult<ContractAccountVO> queryHfListByGD(PageQuery query, Integer page, Integer rowNum) throws Exception {
        var list = getConstructionContractSN();
        var betonSN = HsApp.getSystemSet(CK.BetonSysCode);
        var glpsSN = HsApp.getSystemSet(CK.AggregateTakeoutSN);
        query.getQuery().put("contractListSN",list);
        var saleContractList = saleContractService.queryPageByProductCateSn(query, page, rowNum);
        if (saleContractList.getData() == null) {
            throw new Exception("未找到相关合同信息");
        }
        List<ContractAccountVO> contractAccountList = new ArrayList<>();
        saleContractList.getData().forEach(t -> {
            ContractAccountVO accountEntity = new ContractAccountVO();
            accountEntity.setContractSn(t.getSn());
            accountEntity.setCustomerName(t.getCustomerName());
            var  shipSettleMain = brickShipSettleMainDao.LastBrickSettle(t.getSn());
            if (shipSettleMain != null) {
                accountEntity.setReceivableAmount(shipSettleMain.getTotalAmount());
                accountEntity.setTotalQuantity(shipSettleMain.getCurrentVolume());
            } else {
                accountEntity.setReceivableAmount(BigDecimal.ZERO);
                accountEntity.setTotalQuantity(BigDecimal.ZERO);
            }
            accountEntity.setInvoiceAmount(invoiceRecordService.sumInvoiceAmountByContractSn(t.getSn()));
            accountEntity.setReceivedAmount(paymentRecordService.sumAmountByContractSn(t.getSn()));
            accountEntity.setUncollectedAmount(accountEntity.getReceivableAmount().subtract(accountEntity.getReceivedAmount()));
            var shipOrderList = brickShipOrderDao.queryListByContractSN(t.getSn());
            BigDecimal unsettledQuantity = BigDecimal.ZERO;
            BigDecimal unsettledAmount = BigDecimal.ZERO;
            for (var shipOrder : shipOrderList) {
                BigDecimal quantity;
                var amounts = shipOrder.getPrice();
                if (shipOrder.getState() >= 10) {
                    if (betonSN != null && betonSN.equals(shipOrder.getProductCateSN())) {
                        quantity = shipOrder.getShipmentReviewQuantity();
                    } else {
                        quantity = shipOrder.getShipmentReviewQuantity();
                    }
                    if (glpsSN != null && glpsSN.equals(shipOrder.getProductCateSN())) {
                        if (shipOrder.getShipmentReviewPrice() != null && shipOrder.getShipmentReviewPrice().doubleValue() > 0) {
                            amounts = shipOrder.getShipmentReviewPrice();
                        }
                    }
                } else if (shipOrder.getState() == 8) {
                    if (betonSN != null && betonSN.equals(shipOrder.getProductCateSN())) {
                        quantity = shipOrder.getSignedQuantity();
                    } else {
                        quantity = shipOrder.getSignedQuantity();
                    }
                } else {
                    if (betonSN != null && betonSN.equals(shipOrder.getProductCateSN())) {
                        quantity = shipOrder.getQuantity();
                    } else {
                        quantity = shipOrder.getQuantity();
                    }
                }
                if (quantity == null) {
                    quantity = BigDecimal.ZERO;
                    log.info(shipOrder.getSn() + "-" + shipOrder.getShipmentReviewFlag() + "-" + shipOrder.getSigner());
                }
                unsettledQuantity = unsettledQuantity.add(quantity);

                BigDecimal amount = amounts == null ? BigDecimal.valueOf(0) : amounts.multiply(quantity);
                unsettledAmount = unsettledAmount.add(amount != null ? amount : BigDecimal.ZERO);
            }
            accountEntity.setUnsettledQuantity(unsettledQuantity);
            accountEntity.setUnsettledAmount(unsettledAmount);
            accountEntity.setContractBalance(accountEntity.getReceivedAmount().subtract(accountEntity.getReceivableAmount()).subtract(accountEntity.getUnsettledAmount()).subtract(detailDao.findByContractSnMoney(t.getSn())));
            contractAccountList.add(accountEntity);
        });
        PageResult<ContractAccountVO> result = new PageResult(rowNum);
        result.setRecords(saleContractList.getRecords());
        result.setData(contractAccountList);
        result.setTotal(result.getTotal());
        result.setPage(page);
        return result;
    }

    public BigDecimal getContractBalance(String contractSN, Date startTime) {
        var shipOrderList = productShipOrderService.queryByContractSn(contractSN);
        var list=shipOrderList.stream().filter(t-> t.getOutTime().compareTo(startTime)>=0).toList();
        BigDecimal unsettledQuantity = BigDecimal.ZERO;
        BigDecimal unsettledAmount = BigDecimal.ZERO;
        String betonSN = "";
        String glpsSN = "";
        var betonAppSetting = appSettingDao.getByParamCode(CK.BetonSysCode);
        var glpsAppSetting = appSettingDao.getByParamCode(CK.AggregateTakeoutSN);
        if (betonAppSetting != null) {
            betonSN = betonAppSetting.getParamValue();
        }
        if (glpsAppSetting != null) {
            glpsSN = glpsAppSetting.getParamValue();
        }
        BigDecimal receivableAmount = BigDecimal.ZERO;
        ShipSettleMainEntity shipSettleMain = shipSettleService.findLastByContractSn(contractSN);
        if (shipSettleMain != null) {
            receivableAmount = shipSettleMain.getTotalAmount();
        }
        var receivedAmount = paymentRecordService.sumAmountByContractSn(contractSN);
        var uncollectedAmount = receivableAmount.subtract(receivedAmount);
        for (var shipOrder : list) {
            BigDecimal quantity;
            var amounts = shipOrder.getPrice();
            if (shipOrder.getState() >= 10) {
                if (betonSN != null && betonSN.equals(shipOrder.getProductCateSN())) {
                    quantity = shipOrder.getShipmentReviewQuantity();
                } else {
                    quantity = shipOrder.getShipmentReviewWeight();
                }
                if (glpsSN != null && glpsSN.equals(shipOrder.getProductCateSN())) {
                    if (shipOrder.getShipmentReviewPrice() != null && shipOrder.getShipmentReviewPrice().doubleValue() > 0) {
                        amounts = shipOrder.getShipmentReviewPrice();
                    }
                }
            } else if (shipOrder.getState() == 8) {
                if (betonSN != null && betonSN.equals(shipOrder.getProductCateSN())) {
                    quantity = shipOrder.getSignedQuantity();
                } else {
                    quantity = shipOrder.getSignedWeight();
                }
            } else {
                if (betonSN != null && betonSN.equals(shipOrder.getProductCateSN())) {
                    quantity = shipOrder.getQuantity();
                } else {
                    quantity = shipOrder.getNetWeight();
                }
            }
            if (quantity == null) {
                quantity = BigDecimal.ZERO;
                log.info(shipOrder.getSn() + "-" + shipOrder.getShipmentReviewFlag() + "-" + shipOrder.getSigner());
            }
            unsettledQuantity = unsettledQuantity.add(quantity);

            BigDecimal amount = amounts == null ? BigDecimal.valueOf(0) : amounts.multiply(quantity);
            unsettledAmount = unsettledAmount.add(amount != null ? amount : BigDecimal.ZERO);
        }

        var contractBalance = receivedAmount.subtract(receivableAmount).subtract(uncollectedAmount);
        return contractBalance;
    }

    @Override
    public PageResult<ContractAccountVO> queryListConstruction(PageQuery query, Integer page, Integer rowNum) throws Exception {
        var betonSN = HsApp.getSystemSet(CK.BetonSysCode);
        if (query.getQuery().get("productCateSn") == null) {
            throw new Exception("搜索条件中缺少productCateSn");
        }
        var saleContractList = saleContractService.queryPageConstruction(query, page, rowNum, query.getQuery().get("productCateSn").toString());
        if (saleContractList.getData() == null) {
            throw new Exception("未找到相关合同信息");
        }
        List<ContractAccountVO> contractAccountList = new ArrayList<>();
        saleContractList.getData().forEach(t -> {
            ContractAccountVO accountEntity = new ContractAccountVO();
            accountEntity.setContractSn(t.getSn());
            accountEntity.setCustomerName(t.getCustomerName());
            ShipSettleMainEntity shipSettleMain = shipSettleService.findLastByContractSn(t.getSn());
            if (shipSettleMain != null) {
                accountEntity.setReceivableAmount(shipSettleMain.getTotalAmount());
                accountEntity.setTotalQuantity(shipSettleMain.getTotalQuantity());
            } else {
                accountEntity.setReceivableAmount(BigDecimal.ZERO);
                accountEntity.setTotalQuantity(BigDecimal.ZERO);
            }
            accountEntity.setInvoiceAmount(invoiceRecordService.sumInvoiceAmountByContractSn(t.getSn()));
            accountEntity.setReceivedAmount(paymentRecordService.sumAmountByContractSn(t.getSn()));
            accountEntity.setUncollectedAmount(accountEntity.getReceivableAmount().subtract(accountEntity.getReceivedAmount()));
//            if(accountEntity.getUncollectedAmount().compareTo(BigDecimal.ZERO)<0){
//                accountEntity.setUncollectedAmount(BigDecimal.ZERO);
//            }
            var shipOrderList = productShipOrderService.queryByContractSn(t.getSn());
            BigDecimal unsettledQuantity = BigDecimal.ZERO;
            BigDecimal unsettledAmount = BigDecimal.ZERO;
            for (var shipOrder : shipOrderList) {
                BigDecimal quantity;
                var amounts = shipOrder.getPrice();
                if (shipOrder.getState() == 10) {
                    if (betonSN != null && betonSN.equals(shipOrder.getProductCateSN())) {
                        quantity = shipOrder.getShipmentReviewQuantity();
                    } else {
                        quantity = shipOrder.getShipmentReviewWeight();
                    }
                } else if (shipOrder.getState() == 8) {
                    if (betonSN != null && betonSN.equals(shipOrder.getProductCateSN())) {
                        quantity = shipOrder.getSignedQuantity();
                    } else {
                        quantity = shipOrder.getSignedWeight();
                    }
                } else {
                    if (betonSN != null && betonSN.equals(shipOrder.getProductCateSN())) {
                        quantity = shipOrder.getQuantity();
                    } else {
                        quantity = shipOrder.getNetWeight();
                    }
                }
                if (quantity == null) {
                    quantity = BigDecimal.ZERO;
                    log.info(shipOrder.getSn() + "-" + shipOrder.getShipmentReviewFlag() + "-" + shipOrder.getSigner());
                }
                unsettledQuantity = unsettledQuantity.add(quantity);

                BigDecimal amount = amounts == null ? BigDecimal.valueOf(0) : amounts.multiply(quantity);
                unsettledAmount = unsettledAmount.add(amount != null ? amount : BigDecimal.ZERO);
            }
            accountEntity.setUnsettledQuantity(unsettledQuantity);
            accountEntity.setUnsettledAmount(unsettledAmount);
            accountEntity.setContractBalance(accountEntity.getReceivedAmount().subtract(accountEntity.getReceivableAmount()).subtract(accountEntity.getUnsettledAmount()).subtract(detailDao.findByContractSnMoney(t.getSn())));
            //accountEntity.setContractBalance(accountEntity.getReceivedAmount().subtract(accountEntity.getReceivableAmount()).subtract(accountEntity.getUnsettledAmount()));
            contractAccountList.add(accountEntity);
        });
        PageResult<ContractAccountVO> result = new PageResult(rowNum);
        result.setRecords(saleContractList.getRecords());
        result.setData(contractAccountList);
        result.setTotal(result.getTotal());
        result.setPage(page);
        return result;
    }

    @Override
    public ContractAccountVO queryListConstruction(String contractSN) {
        var betonSN = HsApp.getSystemSet(CK.BetonSysCode);
        var glpsSN = HsApp.getSystemSet(CK.AggregateTakeoutSN);
        var saleContractentity = saleContractService.findBySn(contractSN);
            ContractAccountVO accountEntity = new ContractAccountVO();
            accountEntity.setContractSn(saleContractentity.getSn());
            accountEntity.setCustomerName(saleContractentity.getCustomerName());
            var  shipSettleMain = brickShipSettleMainDao.LastBrickSettle(saleContractentity.getSn());
            if (shipSettleMain != null) {
                accountEntity.setReceivableAmount(shipSettleMain.getTotalAmount());
                accountEntity.setTotalQuantity(shipSettleMain.getCurrentVolume());
            } else {
                accountEntity.setReceivableAmount(BigDecimal.ZERO);
                accountEntity.setTotalQuantity(BigDecimal.ZERO);
            }
            accountEntity.setInvoiceAmount(invoiceRecordService.sumInvoiceAmountByContractSn(saleContractentity.getSn()));
            accountEntity.setReceivedAmount(paymentRecordService.sumAmountByContractSn(saleContractentity.getSn()));
            accountEntity.setUncollectedAmount(accountEntity.getReceivableAmount().subtract(accountEntity.getReceivedAmount()));
            var shipOrderList = brickShipOrderDao.queryListByContractSN(saleContractentity.getSn());
            BigDecimal unsettledQuantity = BigDecimal.ZERO;
            BigDecimal unsettledAmount = BigDecimal.ZERO;
            for (var shipOrder : shipOrderList) {
                BigDecimal quantity;
                var amounts = shipOrder.getPrice();
                if (shipOrder.getState() >= 10) {
                    if (betonSN != null && betonSN.equals(shipOrder.getProductCateSN())) {
                        quantity = shipOrder.getShipmentReviewQuantity();
                    } else {
                        quantity = shipOrder.getShipmentReviewQuantity();
                    }
                    if (glpsSN != null && glpsSN.equals(shipOrder.getProductCateSN())) {
                        if (shipOrder.getShipmentReviewPrice() != null && shipOrder.getShipmentReviewPrice().doubleValue() > 0) {
                            amounts = shipOrder.getShipmentReviewPrice();
                        }
                    }
                } else if (shipOrder.getState() == 8) {
                    if (betonSN != null && betonSN.equals(shipOrder.getProductCateSN())) {
                        quantity = shipOrder.getSignedQuantity();
                    } else {
                        quantity = shipOrder.getSignedQuantity();
                    }
                } else {
                    if (betonSN != null && betonSN.equals(shipOrder.getProductCateSN())) {
                        quantity = shipOrder.getQuantity();
                    } else {
                        quantity = shipOrder.getQuantity();
                    }
                }
                if (quantity == null) {
                    quantity = BigDecimal.ZERO;
                    log.info(shipOrder.getSn() + "-" + shipOrder.getShipmentReviewFlag() + "-" + shipOrder.getSigner());
                }
                unsettledQuantity = unsettledQuantity.add(quantity);

                BigDecimal amount = amounts == null ? BigDecimal.valueOf(0) : amounts.multiply(quantity);
                unsettledAmount = unsettledAmount.add(amount != null ? amount : BigDecimal.ZERO);
            }
            accountEntity.setUnsettledQuantity(unsettledQuantity);
            accountEntity.setUnsettledAmount(unsettledAmount);
            accountEntity.setContractBalance(accountEntity.getReceivedAmount().subtract(accountEntity.getReceivableAmount()).subtract(accountEntity.getUnsettledAmount()).subtract(detailDao.findByContractSnMoney(saleContractentity.getSn())));
            //accountEntity.setContractBalance(accountEntity.getReceivedAmount().subtract(accountEntity.getReceivableAmount()).subtract(accountEntity.getUnsettledAmount()));
           return accountEntity;
    }
}